#include <iostream>
#include <jsoncpp/json/json.h>
#include "compile_run.hpp"
#include "../comm/httplib.h"

using namespace ns_compile_run;
using namespace httplib;

void Usage(std::string str)
{
    std::cerr<<"Usage is "<<str<<" + port"<<std::endl;
}

int main(int argc,char* argv[])
{
    if(argc!=2)
    {
        Usage(argv[0]);
        return -1;
    }
    Server svr;
    int port = atoi(argv[1]);
    svr.Post("/compile_and_run",[](const Request& req,Response& resp){
        std::string in_json = req.body;
        std::string out_json;
        if(!in_json.empty())
        {
            compile_run::Start(in_json,&out_json);
            resp.set_content(out_json,"application/json;charset=utf-8");
        }
    });

    svr.listen("0.0.0.0",port);
    // Json::Value in_value;
    // in_value["code"] = R"(
    //     #include <iostream>
    //     int main()
    //     {
    //         std::cout<<"hello bit"<<std::endl;
    //     }
    // )";
    // in_value["input"]="";
    // in_value["cpu_limit"]=1;
    // in_value["mem_limit"]=1024*10;

    // Json::StreamWriterBuilder writer;
    // writer["emitUTF8"] = true;    // Ensure UTF-8 output
    // std::string in_json = Json::writeString(writer,in_value);
    // std::cout<<in_json<<std::endl;
    
    // std::string out_json;
    // compile_run::Start(in_json,&out_json);
    // std::cout<<out_json<<std::endl;
    return 0;
}